package com.yonyou.ui;

import com.intellij.openapi.ui.Messages;
import com.yonyou.common.dialog.CustomDialogWrapper;
import com.yonyou.component.MultiComboBox;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.LinkedHashMap;

/**
 * <p>
 *
 * @author fengjiening
 * @since 2024-01-29
 */
public class GitChrryPickFrom {
    private JPanel north = new JPanel();
    private JPanel center = new JPanel();
    private JPanel south = new JPanel();

    private JLabel branch  = new JLabel("分支：");
    private JLabel flag = new JLabel("标记：");
    private JLabel pwd = new JLabel("秘钥：");
    private JComboBox<String> branch_box = new JComboBox<String>();
    //private JComboBox branch_box=new JComboBox();
    private JTextArea flag_text= new JTextArea();
    private JTextArea pwd_text= new JTextArea();
    public static int text_h=35;
    private MultiComboBox multiComboBox = new MultiComboBox();
    JButton sureBtn = new JButton("确认");
    JButton cancleBtn = new JButton("取消");

    /**
     * 返回位于会话框north位置的swing样式
     */
    public JComponent initNorth() {
        //定义表单的标题部分，放置到IDEA会话框的顶部位置
        JLabel title = new JLabel("CherryPickTool");
        title.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //字体样式

        north.add(title,BorderLayout.WEST);
        //不设置标题// 字在中间难看
        return null;
    }

    /**
     * 定义表单的主题，放置到IDEA会话框的中央位置
     * @return
     */
    public JComponent initCenter() {
        java.util.LinkedHashMap list = new LinkedHashMap<>();
        list.put("全选","全选");
        list.put("aaa1","haha1");
        list.put("aaa2","haha2");
        list.put("aaa3","haha3");
        list.put("aaa3","aaa3");

        final GridBagLayout gridLayout = new GridBagLayout();
        center.setLayout(gridLayout);


        GridBagConstraints g1 = new GridBagConstraints();
        g1.gridx=1;
        g1.gridy=1;

        //row1：按钮事件触发后将结果打印在这里
        branch.setForeground(new Color(255, 47, 93)); //设置字体颜色
        branch.setHorizontalAlignment(SwingConstants.CENTER);
        center.add(branch,g1);


        GridBagConstraints g2 = new GridBagConstraints();
        g2.gridx=2;
        g2.gridy=1;
        multiComboBox.initComponent(list);
        center.add(multiComboBox,g2);


        GridBagConstraints g3 = new GridBagConstraints();
        g3.gridx=1;
        g3.gridy=2;
        g3.insets = new Insets(20,0,20,0);

        flag.setForeground(new Color(139, 181, 20)); //设置字体颜色
        flag.setHorizontalAlignment(SwingConstants.CENTER);
        flag.setVerticalAlignment(SwingConstants.TOP);
        center.add(flag,g3);

        GridBagConstraints g4 = new GridBagConstraints();
        g4.gridx=2;
        g4.gridy=2;
        g4.insets = new Insets(20,0,20,0);
        flag_text.setLineWrap(true);//设置换行
        flag_text.setPreferredSize(new Dimension(220,80));




        GridBagConstraints g5 = new GridBagConstraints();
        g5.gridx=1;
        g5.gridy=3;
        g5.insets = new Insets(20,0,20,0);
        pwd.setForeground(new Color(139, 181, 20)); //设置字体颜色
        pwd.setHorizontalAlignment(SwingConstants.CENTER);
        pwd.setVerticalAlignment(SwingConstants.TOP);
        center.add(pwd,g5);

        GridBagConstraints g6 = new GridBagConstraints();
        g6.gridx=2;
        g6.gridy=3;
        g6.insets = new Insets(20,0,20,0);
        pwd_text.setLineWrap(true);//设置换行
        pwd_text.setPreferredSize(new Dimension(220,80));
        center.add(pwd_text,g6);



        return center;
    }
    /**
     *  特别说明：不需要展示SouthPanel要重写返回null，
     *  否则IDEA将展示默认的"Cancel"和"OK"按钮
     * @return
     */
    public JComponent initSouth(CustomDialogWrapper wapper) {
        sureBtn.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        sureBtn.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        south.add(sureBtn, BorderLayout.EAST);

        cancleBtn.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        cancleBtn.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        south.add(cancleBtn, BorderLayout.EAST);


        //按钮事件绑定
        sureBtn.addActionListener(e -> {
            //获取到name和age
            final String[] selectedValues = multiComboBox.getSelectedValues();

            // 弹出模态框显示以下的消息
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Messages.showMessageDialog(String.join("-",selectedValues), "提示", null);
                }
            });
        });
        cancleBtn.addActionListener(e -> {
            wapper.doCancelAction();
        });


        return south;
    }


}
